//
//  PlayVC.m
//  pyq
//
//  Created by dev-oo on 2017/10/19.
//  Copyright © 2017年 pingmei. All rights reserved.
//

#import "PlayVC.h"
#import "HMPVolumeView.h"
#import <MediaPlayer/MediaPlayer.h>

@interface PlayVC ()
@property (strong, nonatomic) ZFPlayerView *zfPlayView;
@property (weak, nonatomic) IBOutlet UIView *playView;
//将视频镜像到支持airplay的设备上的view按钮
@property(nonatomic,strong) HMPVolumeView *airplayView;
@end

@implementation PlayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ZFPlayerControlView *controlView = [[ZFPlayerControlView alloc] init];
     self.zfPlayView = [[ZFPlayerView alloc] init];
    // 初始化播放模型
    ZFPlayerModel *playerModel = [[ZFPlayerModel alloc] init];
    // playerView的父视图
    playerModel.fatherView = self.playView;
    playerModel.videoURL = [NSURL URLWithString:self.videoUrl];
    playerModel.title = self.videoTitle;
    
    self.zfPlayView.hasDownload = YES;
    [self.zfPlayView playerControlView:controlView playerModel:playerModel];
    // 自动播放
    [self.zfPlayView autoPlayTheVideo];
    // 设置代理
    self.zfPlayView.delegate = self;
    
    [self interfaceOrientation:UIInterfaceOrientationLandscapeLeft];
    
    _airplayView = [[HMPVolumeView alloc] initWithFrame:CGRectMake(ScreenWidth - 70, 15, 50, 50)];
    _airplayView.showsVolumeSlider = NO;
    [self.airplayView setRouteButtonImage:[UIImage imageNamed:@"airPlay"] forState:UIControlStateNormal];
//    self.airplayView.hidden = YES;
    [controlView.topImageView addSubview:_airplayView];
    
}


-(void)activeInfo{
    if (self.airplayView.isWirelessRouteActive) {
        NSLog(@"已经连上电视");
    }else{
        NSLog(@"断开连接");
    }
}
-(void)availableInfo{
    if (self.airplayView.areWirelessRoutesAvailable) {
        NSLog(@"可以搜到设备");
        self.airplayView.hidden = NO;
    }else{
        self.airplayView.hidden = YES;
        NSLog(@"没有搜索到设备");
    }
}

/** 返回按钮事件 */
- (void)zf_playerBackAction{
    [self.zfPlayView removeFromSuperview];
    self.zfPlayView = nil;
    [self.navigationController popViewControllerAnimated:YES];
}


/**
 *  强制屏幕转屏
 *
 *  @param orientation 屏幕方向
 */
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{
    // arc下
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = orientation;
        // 从2开始是因为0 1 两个参数已经被selector和target占用
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    // 直接调用这个方法通不过apple上架审核
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
